package com.example.ossdemo.util;

import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClient;
import com.aliyun.oss.model.GeneratePresignedUrlRequest;
import lombok.Data;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;

import java.net.URL;
import java.util.Date;
//@Component
public class OSSUtil3 {
    @Value("${aliyun.oss.aliyunDomain}")
    private static String ENDPOINT;
    @Value("${aliyun.oss.aliyunAccessKeyId}")
    private static String ACCESSKEYID;
    @Value("${aliyun.oss.aliyunAccessKeySecret}")
    private static String ACCESSKETSECRET;
    @Value("${aliyun.oss.aliyunBucketName}")
    private static String BUCKETNAME;

//    @Bean
    public  String getUrl(String key){

        OSS ossClient = new OSSClient(ENDPOINT, ACCESSKEYID, ACCESSKETSECRET);
        // 设置URL过期时间为1小时
        Date expiration = new Date(new Date().getTime() + 20000 );
        GeneratePresignedUrlRequest generatePresignedUrlRequest ;
        generatePresignedUrlRequest =new GeneratePresignedUrlRequest(BUCKETNAME, key);
        generatePresignedUrlRequest.setExpiration(expiration);
        URL url = ossClient.generatePresignedUrl(generatePresignedUrlRequest);
        return url.toString();
    }
}
